/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rsds;

import bsh.EvalError;
import bsh.Interpreter;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.beans.JLineChart;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.DatasetChangeListener;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author ahmet
 */


public class EulerMaruyama {
    private XYSeries r, x1, x2;
    private XYSeriesCollection colx, colr;
    private double t, x1t, x2t, rt;

    private double nextjumpt; 

    public EulerMaruyama(final JLineChart lcr, final JLineChart lcx, double r0, double x10, double x20) {
        t = 0;
        x1t = x10;
        x2t = x20;
        x1 = new XYSeries("x1");
        x2 = new XYSeries("x2");

        x1.setMaximumItemCount(500);
        x2.setMaximumItemCount(500);
        x1.addOrUpdate(0, x10);
        x2.addOrUpdate(0, x20);
        colx = new XYSeriesCollection();
        colx.addSeries(x1);
        colx.addSeries(x2);
        lcx.setDataset(colx);

        colx.addChangeListener(new DatasetChangeListener() {
            public void datasetChanged(DatasetChangeEvent arg0) {
                lcx.repaint();
            }
        });


        r = new XYSeries("r");
        r.setMaximumItemCount(500);
        r.addOrUpdate(0, r0);
        rt=r0;
        colr = new XYSeriesCollection();
        colr.addSeries(r);
        lcr.setDataset(colr);

        colr.addChangeListener(new DatasetChangeListener() {
            public void datasetChanged(DatasetChangeEvent arg0) {
                lcr.repaint();
            }
        }); 

        nextjumpt = 2*Math.random();
    }

    public void update(Vector<String> subsystemEquations,  double deltat) {

        //System.out.println(rt);
        int rv = (int)rt;
        //System.out.println(rv);
        String eq = subsystemEquations.elementAt(rv-1);
        String x1eq = eq.split(",")[0];
        String x2eq = eq.split(",")[1];

        //x1eq = x1eq.replaceAll("x1", ""+x1t);
        //x1eq = x1eq.replaceAll("x1", ""+x1t);
        //x2eq = x2eq.replaceAll("x1", ""+x1t);
        //x2eq = x2eq.replaceAll("x2", ""+x2t);

        Interpreter i = new Interpreter();  // Construct an interpreter
        double newx1t = x1t;
        double newx2t = x2t;
        // Eval a statement and get the result
        try {
            i.eval("x1="+x1t);
            i.eval("x2="+x2t);
            i.eval("newx1=("+x1eq+")*"+deltat);
            i.eval("newx2=("+x2eq+")*"+deltat);
            newx1t = (Double)i.get("newx1");
            newx2t = (Double)i.get("newx2");
        } catch (EvalError ex) {
            ex.printStackTrace();
        }

        x1t = x1t+ newx1t;
        x2t = x2t+ newx2t;


        if (t >= nextjumpt) {
            rt = (int) ( 1 + Math.random() *subsystemEquations.size());
            nextjumpt = t+deltat+2*Math.random();
        }
        t += deltat;

        x1.addOrUpdate(t, x1t);
        x2.addOrUpdate(t, x2t);
        r.addOrUpdate(t, rt);
    }
}
